/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAO;

import Model.Connect;
import Pojo.LoaiSanPham;
import Util.HibernateUtil;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import javax.management.Query;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.hql.ast.HqlSqlWalker;

/**
 *
 * @author NTB
 */
import org.hibernate.id.IdentifierGenerator;
import org.hibernate.id.PostInsertIdentifierGenerator;
import org.hibernate.id.SequenceGenerator;
public class ProductCategoryDAO {
    Connect cn;
    
    public static List<LoaiSanPham> View()
    {
        List<LoaiSanPham> list=null;
        Session se=HibernateUtil.getSessionFactory().openSession();
        try
        {
            String sql = "SELECT lbs FROM LoaiSanPham lbs";
            org.hibernate.Query qr= se.createQuery(sql);
            list=qr.list();  
        }
        catch (HibernateException e)
        {
            System.err.print(e);
        }
        finally
        {
            se.close();
        }
        return list;
    }
    
    public static List<LoaiSanPham> Search(String name)
    {
        List<LoaiSanPham> list=null;
        Session se=HibernateUtil.getSessionFactory().openSession();
        try
        {
            String sql="SELECT lbs FROM LoaiSanPham lbs WHERE lbs.tenloaisp like :name";
            org.hibernate.Query qr= se.createQuery(sql);
            qr.setString("name", "%"+name+"%");
            list=qr.list();  
        }
        catch (HibernateException e)
        {
            e.printStackTrace();
        }
        finally
        {
            se.close();
        }
        return list;
    }
    
    public static LoaiSanPham GetInfo(int pro_cate_id)
    {
        LoaiSanPham p_c=null;
        Session se=HibernateUtil.getSessionFactory().openSession();
        try
        {
            p_c=(LoaiSanPham) se.get(LoaiSanPham.class, pro_cate_id);
        }
        catch (HibernateException e)
        {
            e.printStackTrace();
        }
        finally
        {
            se.close();
        }
        return p_c;
    }
    
    public static boolean Delete(int pro_cate_id)
    {
        Session se=HibernateUtil.getSessionFactory().openSession();
        LoaiSanPham p_c=ProductCategoryDAO.GetInfo(pro_cate_id);
        if(p_c==null)
        {
            return false;
        }
        Transaction tr=null;
        try
        {
            tr=se.beginTransaction();
            se.delete(p_c);
            tr.commit();
        }
        catch (Exception e)
        {
            tr.rollback();
            e.printStackTrace();
        }finally
        {
            se.close();
        }
        return true;
    }
    
    public static boolean Update(LoaiSanPham p_c)
    {
        Session se=HibernateUtil.getSessionFactory().openSession();        
        if(ProductCategoryDAO.GetInfo(p_c.getMaloaisp()) ==null)
        {
            return false;
        }
        Transaction tr=null;
        try
        {
            tr=se.beginTransaction();
            se.update(p_c);
            tr.commit();
        }
        catch (Exception e)
        {
            tr.rollback();
            e.printStackTrace();
        }finally
        {
            se.close();
        }
        return true;
    }
    
    public static boolean Insert(LoaiSanPham p_c)
    {
        Session se=HibernateUtil.getSessionFactory().openSession();   
        if(ProductCategoryDAO.GetInfo(p_c.getMaloaisp()) !=null)
        {
            return false;
        }
        Transaction tr=null;
        try
        {
            tr=se.beginTransaction();
            se.save(p_c);
            tr.commit();
        }
        catch (Exception e)
        {
            tr.rollback();
            e.printStackTrace();
        }finally
        {
            se.close();
        }
        return true;
    }
    public static boolean supportsIdGenWithBulkInsertion(IdentifierGenerator generator) {
        return SequenceGenerator.class.isAssignableFrom( generator.getClass() )
            || PostInsertIdentifierGenerator.class.isAssignableFrom( generator.getClass() );
    }
}
